package org.darkforest.common.services.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.darkforest.common.services.*;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.JSONUtils;
import org.darkforest.dao.mapper.*;
import org.darkforest.model.*;
import org.darkforest.vo.StoreClassVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2015/12/18.
 */
@Service
@Transactional
public class StoreSelfSupportingServiceImpl implements StoreSelfSupportingService {

    @Autowired
    private MemberService memberService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private SellerService sellerService;

    @Autowired
    private MemberMsgSettingMapper memberMsgSettingMapper;

    @Autowired
    private StoreJoinInService storeJoinInService;
    @Autowired
    private SellerGroupMapper sellerGroupMapper;
    @Autowired
    private AlbumClassMapper albumClassMapper;

    @Autowired
    private StoreBindClassService storeBindClassService;

    @Override
    public void addSelfSupporting(Store storeInfo) {
        /************************用户中心数据添加*****************************/
        MemberWithBLOBs member = new MemberWithBLOBs();
        member.setMemberName(storeInfo.getMemberName());
        member.setMemberPasswd(DigestUtils.md5Hex(storeInfo.getMemberPasswd()));
        member.setMemberEmail(" "); //添加自营店铺默认邮箱
        member.setMemberEmailBind(0);
        member.setMemberMobile(" "); //添加自营店铺默认电话
        member.setMemberMobileBind(0);
        member.setMemberLoginNum(0);
        member.setMemberLoginTime(DateUtil.unixTimeStampInt() + "");
        member.setMemberTime(DateUtil.unixTimeStampInt() + "");
        member.setMemberOldLoginTime(DateUtil.unixTimeStampInt() + "");
        memberService.insertSelective(member);

        MemberMsgSetting memberMsgSetting = new MemberMsgSetting();
        memberMsgSetting.setMemberId(member.getMemberId());
        memberMsgSetting.setIsReceive(true);
        memberMsgSetting.setMmtCode("return_auto_receipt");
        memberMsgSettingMapper.insert(memberMsgSetting);


        /*******************自营店铺添加**********************/
        StoreWithBLOBs store = new StoreWithBLOBs();
        store.setSellerName(storeInfo.getSellerName());
        store.setStoreName(storeInfo.getStoreName());
        //    store.setGradeId(1); //添加自营店铺默认选择
        store.setMemberName(storeInfo.getMemberName());
        store.setMemberId(member.getMemberId());
        store.setScId(storeInfo.getScId()); //0 默认
        store.setProvinceId(storeInfo.getProvinceId());
        store.setStoreState(1);
        store.setCommissionRate(storeInfo.getCommissionRate());
        store.setCountyId(storeInfo.getCountyId());
        store.setCompanyId(storeInfo.getCompanyId());
        storeService.insertSelective(store);

        SellerGroupWithBLOBs sg = new SellerGroupWithBLOBs();
        sg.setStoreId(store.getStoreId());
        sg.setGroupName("店铺默认权限");
        sg.setLimits("[10, 20, 201, 202, 203, 206, 30, 31, 32, 33, 34, 40, 43, 60, 61, 62, 90, 92, 93, 100, 101]");
        //sg.setSmtLimits("[complain, goods_storage_alarm, goods_verify, goods_violation, new_order, refund, refund_auto_process, return, return_auto_process, return_auto_receipt, store_bill_affirm, store_bill_gathering, store_cost, store_expire]");
        sellerGroupMapper.insertSelective(sg);


        /***************商户数据添加******************/
        Seller seller = new Seller();
        seller.setSellerName(storeInfo.getSellerName());
        seller.setMemberId(member.getMemberId());
        seller.setStoreId(store.getStoreId());
        seller.setIsAdmin((byte) 1);
        seller.setSellerGroupId(sg.getGroupId());

        sellerService.insertSelective(seller);



        /***************商户入驻信息******************/
        StoreJoinIn sj = new StoreJoinIn();
        sj.setScId(store.getScId());
        sj.setMemberId(member.getMemberId());
        sj.setSellerName(seller.getSellerName());
        sj.setStoreId(store.getStoreId());
        sj.setCompanyProvinceId(store.getProvinceId());
        sj.setMemberName(storeInfo.getMemberName());
        sj.setStoreName(storeInfo.getStoreName());
        sj.setJoininState("11");
        sj.setStoreClassIds(JSONUtils.toJSONString(storeInfo.getToreClassVos()));
        storeJoinInService.insertSelective(sj);

        /***************商户数据添加******************/
        StoreBindClass storeBindClass = new StoreBindClass();
        storeBindClass.setStoreId(store.getStoreId());
        if(sj.getStoreClassIds()!= null){
            List<StoreClassVo> list= JSONUtils.toList(storeInfo.getToreClassVos(), StoreClassVo.class);
            for(StoreClassVo storeClass:list){
                if(storeClass!=null&&storeClass.getStoreClassId() != null && !storeClass.getStoreClassId().equals("")){
                    if(storeClass.getStoreClassCommisRate()!=null&&!storeClass.getStoreClassCommisRate().equals("")&&Double.parseDouble(storeClass.getStoreClassCommisRate().trim())>0){
                        storeBindClass.setCommisRate(new BigDecimal(storeClass.getStoreClassCommisRate()));
                    }else{
                        storeBindClass.setCommisRate(new BigDecimal(0));
                    }
                    String str[] = storeClass.getStoreClassId().split(",");
                    if(str.length>0){
                        if(str[0]!=null){
                            storeBindClass.setClass1(Integer.parseInt(str[0]));
                            if(str.length>1&&str[1]!=null){
                                storeBindClass.setClass2(Integer.parseInt(str[1]));
                                if(str.length>2&&str[2]!=null){
                                    storeBindClass.setClass3(Integer.parseInt(str[2]));
                                }
                            }
                        }
                    }
                    storeBindClass.setState((byte)1);
                    storeBindClassService.addStoreBindStoreGoodsClass(storeBindClass);
                }
            }
        }

        /*****商户图片空间****/
        AlbumClass albumClass = new AlbumClass();
        albumClass.setStoreId(store.getStoreId());
        albumClass.setAclassName("默认相册");
        albumClass.setAclassDes("商户默认相册");
        albumClass.setAclassSort((byte) 33);
        albumClass.setUploadTime(DateUtil.unixTimeStamp());
        albumClass.setIsDefault(1);
        albumClass.setAclassCover("");
        albumClassMapper.insert(albumClass);
    }

    /**
     * 根据店铺ID 删除自营店铺
     *
     * @param storeId
     */
    @Override
    public void delSelfSupportingByStoreId(Integer storeId) {
        Store store = storeService.findStoreInfoByPrimary(storeId);
        //删除用户表数据
        memberService.deleteByPrimaryKey(store.getMemberId());
        //删除用户消息设置表数据
        memberMsgSettingMapper.deleteByStoreId(storeId);
        //删除店铺表数据
        storeService.deleteByPrimaryKey(storeId);
        //根据店铺id删除用户组
        sellerGroupMapper.deleteByStoreId(storeId);
        //删除商户中心用户
        sellerService.deleteByStoreId(storeId);
        //删除商户申请表数据
        storeJoinInService.deleteByPrimaryKey(store.getMemberId());

    }
}
